<?php
class SystemLog extends ModelBase
{
	private $tableName = 'myadmin_sys_log';
	
	public static function getInstance()
	{
		return parent::instance(__CLASS__);
	}
	
	public function addLog($data)
	{
		$data['op_time'] = time();
		$ret = Yii::app()->db->createCommand()->insert($this->tableName,$data);
		return $ret; 
	}
	
    public function getLogCount($type,$startTime,$endTime,$user_name)
    {
    	$where = ' 1 ';
    	if ($type) $where .=' and class_name = "'.$type."\"";
    	if ($startTime) {
    		$startTime = strtotime($startTime);
    		$where .=" and op_time >= $startTime";
    	}
    	if ($endTime) {
    		$endTime = strtotime($endTime);
    		$where .=" and op_time <= $endTime ";
    	}
    	
    	if($user_name)
    	{
    		$where .=' and user_name <= "'.$user_name.'"';
    	}
    	$count = Yii::app()->db->createCommand()
    	->select('count(*) as num ')
    	->from($this->tableName)
    	->where($where)
    	->queryRow();
    	return $count['num'];
    }
	
	public function getLog($type,$startTime,$endTime,$user_name,$limit,$offset)
	{   		
		$where = ' 1 ';
		if ($type) $where .=' and class_name = "'.$type."\"";
		if ($startTime) {
			$startTime = strtotime($startTime);
			$where .=" and op_time >= $startTime ";
		}
		if ($endTime) {
			$endTime = strtotime($endTime);
			$where .=" and op_time <= $endTime ";
		}
		
		if($user_name)
		{
			$where .=' and user_name = "'.$user_name.'"';
		}
		$Log = Yii::app()->db->createCommand()
		->select('*')
		->from($this->tableName)
		->where($where)
		->limit($limit,$offset)
		->queryAll();
		return $Log;
	}
}